#S8 - Simulations to investigate power.

#SIMULATION A
#Assume that 3% willing to support violence, clearly worded 
#4.8% support violence if question is  worded poorly
#Create two panels, panel A and Panel B

#Make hypothetical population A
GRPA<-c(rep(0,.97*10000),rep(1,.03*10000))

#verify
mean(GRPA)

#Make hypothetical population B
GRPB<-c(rep(0,.952*10000),rep(1,.048*10000))

#verify
mean(GRPB)

#Make holder
hldra<-as.data.frame(matrix(nrow=10000,ncol=2))
colnames(hldra)<-c("coef","pval")

#create mock random assignment to Q wording
p_over0<-rep(0,1075)
p_over1<-rep(1,2070)    

#run loop 10,000 times to see how often we see difference
for(i in 1:10000){
  SA<-sample(GRPA,1075,replace=T)
  SB<-sample(GRPB,2070,replace=T)
  
  RC1<-as.data.frame(cbind(p_over0,SA))
  colnames(RC1)<-c("p_over","RTF")
  RC2<-as.data.frame(cbind(p_over1,SB))
  colnames(RC2)<-c("p_over","RTF")
  RU1<-rbind(RC1,RC2)
  RU1<-as.data.frame(RU1)
  
  RLM1<-lm(RTF~p_over,RU1)
  
  RSUM<-summary(RLM1)
  
  hldra[i,2]<-RSUM$coefficients[2,4]
  hldra[i,1]<-RSUM$coefficients[2,1]
  
}

#make histogram
hist(hldra$pval,main="Distribution of p-values for ATE of question wording \n with known 1.8% difference in population \n10,000 sumulated draws",xlab="p-value")

#save histogram as tiff
tiff(filename="../Supplementary Results/FigS8A.tif",height=4,width=6,units="in",res =600)
hist(hldra$pval,main="Distribution of p-values for ATE of question wording \n with known 1.8% difference in population \n10,000 sumulated draws",xlab="p-value")
dev.off()


#find p-values
rx<-table(round(hldra$pval,2))

#identify power
apoint1.power<-sum(rx[1:11])/10000

apoint05.power<-sum(rx[1:6])/10000

apoint001.power<-rx[1]/10000

#group together
ST8AC2<-rbind(apoint1.power,apoint05.power,apoint001.power)
ST8AC1<-c("<.1","<.05","<.01")

#bind together
ST8A<-cbind(ST8AC1,ST8AC2)
colnames(ST8A)<-c("Level of Significance","Percent of Trials Found")

#write out results
write.csv(ST8A,"../Supplementary Results/TableS8A.csv",row.names=FALSE)


#SIMULATION B
#Let's do it again, assuming .03 to .05
#Make sample of US - 3% support for political violence if asked clearly
GRPC<-c(rep(0,.97*10000),rep(1,.03*10000))

#Make sample of US - 5% support for political violence if asked poorly
#Make random sample B
GRPD<-c(rep(0,.95*10000),rep(1,.05*10000))

#Make holder
hldrb<-as.data.frame(matrix(nrow=10000,ncol=2))
colnames(hldrb)<-c("coef","pval")

#create mock random assignment to wording
p_over0<-rep(0,1075)
p_over1<-rep(1,2070)    

#run loop 10,000 times to see how often we see difference
for(i in 1:10000){
  SC<-sample(GRPC,1075,replace=T)
  SD<-sample(GRPD,2070,replace=T)
  
  RC3<-as.data.frame(cbind(p_over0,SC))
  colnames(RC3)<-c("p_over","RTF")
  RC4<-as.data.frame(cbind(p_over1,SD))
  colnames(RC4)<-c("p_over","RTF")
  RU2<-rbind(RC3,RC4)
  RU2<-as.data.frame(RU2)
  
  RLM2<-lm(RTF~p_over,RU2)
  
  RSUM<-summary(RLM2)
  
  hldrb[i,2]<-RSUM$coefficients[2,4]
  hldrb[i,1]<-RSUM$coefficients[2,1]
  
}

#create histogram of values
hist(hldrb$pval,main="Distribution of significance \n 10,000 simulated draws with difference of 2%",xlab="p-value")

#save histogram as tiff
tiff(filename="../Supplementary Results/FigS8B.tif",height=4,width=6,units="in",res =600)
hist(hldrb$pval,main="Distribution of p-values for ATE of question wording \n with known 2% difference in population \n10,000 sumulated draws",xlab="p-value")
dev.off()

#make table
ry<-table(round(hldrb$pval,2))

#Compute Power at .1, .05, .001
bpoint1.power<-sum(ry[1:11])/10000
bpoint05.power<-sum(ry[1:6])/10000
bpoint001.power<-ry[1]/10000

#group together
ST8BC2<-rbind(bpoint1.power,bpoint05.power,bpoint001.power)
ST8BC1<-c("<.1","<.05","<.01")

#bind together
ST8B<-cbind(ST8BC1,ST8BC2)
colnames(ST8B)<-c("Level of Significance","Percent of Trials Found")

#write out results
write.csv(ST8B,"../Supplementary Results/TableS8B.csv",row.names=FALSE)

#SIMULATION C
#Let's do it again, assuming .03 to .04

#Make sample of US - 3% support for political violence if asked clearly
GRPE<-c(rep(0,.97*10000),rep(1,.03*10000))

#Make sample of US - 4% support for political violence if asked poorly
#Make random sample B
GRPF<-c(rep(0,.96*10000),rep(1,.04*10000))

#Make a place to hold estimates
hldrc<-as.data.frame(matrix(nrow=10000,ncol=2))
colnames(hldrc)<-c("coef","pval")

#proportion in each group
p_over0<-rep(0,1075)
p_over1<-rep(1,2070)    

#loop - sample randomly, run regression, save coef estimate and p-value
for(i in 1:10000){
  SE<-sample(GRPE,1075,replace=T)
  SF<-sample(GRPF,2070,replace=T)
  
  RC5<-as.data.frame(cbind(p_over0,SE))
  colnames(RC5)<-c("p_over","RTF")
  RC6<-as.data.frame(cbind(p_over1,SF))
  colnames(RC6)<-c("p_over","RTF")
  RU3<-rbind(RC5,RC6)
  RU3<-as.data.frame(RU3)
  
  RLM3<-lm(RTF~p_over,RU3)
  
  RSUM<-summary(RLM3)
  
  hldrc[i,2]<-RSUM$coefficients[2,4]
  hldrc[i,1]<-RSUM$coefficients[2,1]
  
}

#make histogram of results
hist(hldrc$pval,main="Distribution of significance \n 10,000 simulated draws with difference of 1%",xlab="p-value")

#save histogram as tiff
tiff(filename="../Supplementary Results/FigS8C.tif",height=4,width=6,units="in",res =600)
hist(hldrc$pval,main="Distribution of p-values for ATE of question wording \n with known 1% difference in population \n10,000 sumulated draws",xlab="p-value")
dev.off()

#save results as table
rz<-table(round(hldrc$pval,2))

#Compute Power at .1, .05, .001
cpoint1.power<-sum(rz[1:11])/10000
cpoint05.power<-sum(rz[1:6])/10000
cpoint001.power<-rz[1]/10000

#group together
ST8CC2<-rbind(cpoint1.power,cpoint05.power,cpoint001.power)
ST8CC1<-c("<.1","<.05","<.01")

#bind together
ST8C<-cbind(ST8CC1,ST8CC2)
colnames(ST8C)<-c("Level of Significance","Percent of Trials Found")

#write out results
write.csv(ST8C,"../Supplementary Results/TableS8C.csv",row.names=FALSE)


